package hesso.smartdraw.ch.model;

import java.util.ArrayList;
import java.util.Arrays;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Building implements Parcelable{

	private int id = 0;
	private String name = "default";
	private ArrayList<Level> levels = new ArrayList<Level>();

	public Building(int id, String name){
		this.id = id;
		this.name = name;
	}
	
	public Building(String name){
		this.name = name;
	}
	
	public Building(){
		this("unnamed Building");
	}
	
	public void addGroundLevel(){
		this.levels.add(new Level());
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id){
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public ArrayList<Level> getLevelArray() {
		return levels;
	}

	public void setLevelArray(ArrayList<Level> levels) {
		this.levels = levels;
	}

	public void addLevel(Level level){
		this.levels.add(level);
	}
	
	public void removeLevel(Level level){
		this.levels.remove(level);
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public void writeToParcel(Parcel dest, int flags) {
    	Log.d("tryout", "write Building Parcel");
		dest.writeInt(id);
		dest.writeString(name);
		//dest.writeParcelableArray((Parcelable[])levels.toArray(), 0);		
		dest.writeParcelableArray(levels.toArray(new Level[levels.size()]), 0);
    	Log.d("tryout", "Building Parcel written");
	}
	
	public static final Parcelable.Creator<Building> CREATOR = new Parcelable.Creator<Building>() {
		@Override
		public Building createFromParcel(Parcel source) {
			// TODO Auto-generated method stub
			return new Building(source);
		}

		@Override
		public Building[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Building[size];
		}		
	};
	
	private Building(final Parcel in){
		readFromParcel(in);
	}
	
	public void readFromParcel(final Parcel in){
    	Log.d("tryout", "read from Building Parcel");
		id = in.readInt();
		name = in.readString();
    	Log.d("tryout", "read critical part from Building Parcel");
		levels = (ArrayList<Level>)Arrays.asList((Level[])in.readArray(ClassLoader.getSystemClassLoader()));
    	Log.d("tryout", "read from Building Parcel finished");
	}
}